package com.jiujichina;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;

@Configuration
@PropertySource(value="classpath:httpclient.properties")
public class HttpClientSpringConfig {
	
	 @Value("${http.maxTotal}")
	    private Integer httpMaxTotal;

	    @Value("${http.defaultMaxPerRoute}")
	    private Integer httpDefaultMaxPerRoute;

	    @Value("${http.connectTimeout}")
	    private Integer httpConnectTimeout;

	    @Value("${http.connectionRequestTimeout}")
	    private Integer httpConnectionRequestTimeout;

	    @Value("${http.socketTimeout}")
	    private Integer httpSocketTimeout;

	    @Value("${http.staleConnectionCheckEnabled}")
	    private Boolean httpStaleConnectionCheckEnabled;

	    @Autowired
	    private PoolingHttpClientConnectionManager manager;
	
	
    @Bean
    public PoolingHttpClientConnectionManager poolingHttpClientConnectionManager() {
        PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
        // 最大连接数
        poolingHttpClientConnectionManager.setMaxTotal(httpMaxTotal);
        // 每个主机的最大并发数
        poolingHttpClientConnectionManager.setDefaultMaxPerRoute(httpDefaultMaxPerRoute);
        return poolingHttpClientConnectionManager;
    }
    
    // 定期关闭无效连接
  /*  @Bean
    public IdleConnectionEvictor idleConnectionEvictor() {
        return new IdleConnectionEvictor(manager);
    }*/
    
	@Bean
	@Scope("prototype")
	public CloseableHttpClient closeableHttpClient(){
		return HttpClients.custom().setConnectionManager(manager).build();
	}
	
	// 请求配置
    @Bean
    public RequestConfig requestConfig() {
        return RequestConfig.custom().setConnectTimeout(httpConnectTimeout) // 创建连接的最长时间
                .setConnectionRequestTimeout(httpConnectionRequestTimeout) // 从连接池中获取到连接的最长时间
                .setSocketTimeout(httpSocketTimeout) // 数据传输的最长时间
                .setStaleConnectionCheckEnabled(httpStaleConnectionCheckEnabled) // 提交请求前测试连接是否可用
                .build();
    }
}
